package com.sxpi.zixun.service.user;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sxpi.zixun.bo.UserBo;
import com.sxpi.zixun.mapper.UserMapper;
import com.sxpi.zixun.pojo.User;
import com.sxpi.zixun.vo.UserVo;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @Author: 董伟豪
 * @Date: 2022/6/10 15:56
 */
@Service
@Slf4j
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {


    /**
     * 可以能牵扯到vo - pojo - bo
     * @param userVo
     * @return
     */
    @Override
    public UserBo saveUser(UserVo userVo) {
        User user = tranferBo(userVo, User.class);
        // 保存数据
        this.saveOrUpdate(user);
        return tranferBo(user,UserBo.class);
    }

    @Override
    public List<UserBo> findUserList(UserVo userVo) {
        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
        userQueryWrapper.eq("id",userVo.getId());
        List<User> userList = this.list(userQueryWrapper);
        return tranferListBo(userList,UserBo.class);
    }

    @Override
    public Page<UserBo> pageUser(UserVo userVo) {
        Page<User> page = new Page<>(userVo.getPageNo(), userVo.getPageSize());
        LambdaQueryWrapper<User> userLambdaQueryWrapper = new LambdaQueryWrapper<>();
        userLambdaQueryWrapper.like(StringUtils.isNotBlank(userVo.getUsername()),User::getUsername,userVo.getUsername());
        // 分页返回
        Page<User> userPage = this.page(page, userLambdaQueryWrapper);
        return tranferPageBo(userPage,UserBo.class);
    }

}